Bug 6436220

Bug Description:

This is a race bug. Expected exception is: java.lang.RuntimeException: Error count ...
The attach method in java.nio.channels.SelectionKey is not atomic safe.
More details about this bug are at JDK-6436220 JIRA page.

Interleaving Description:

The numbers in the following code snippets present the global execution sequence.

java.nio.channels.SelectionKey:
	
t1  t2  public final Object attach(Object ob) {
           Object a = attachment;
1   2      attachment = ob;
3   4      return a;
        }

a) thread 1 executes 1, and before it goes to 3, context switched.
b) thread 2 executes 2.
c) context switched, thread 1 continues 3.
d) thread 2 continues 4.
e) When attch is called next time, what was attached in thread 1 is lost, user get the value of what was set in thread 2.

How To Reproduce:

This bug is reproduced under JDK 1.6.0.
It started at JDK 1.6.0, and has been fixed since JDK 1.7.0.
Execute the following scripts to run the test to reproduce the bug (assume the location of the jdk6 test project is jdk_test_home).

Linux:
${jdk_test_home}/scripts/6436220.sh [--javaloc path]
Windows:
%jdk_test_home%\scripts\6436220.bat [--javaloc path]

Option Function
--javaloc The location of JDK that is eligible to reproduce the bug, if your java environment is not eligible.
In this case, it's JDK 1.6.0. It should be the absolute path to the JDK's java starter and ended with "/".
For example: ~/jdk/home/jdk1.6.0/bin/